/*
 * @Author: Eureka 
 * @Date: 2020-09-29 16:16:00 
 * @Last Modified by: Eureka
 * @Last Modified time: 2022-04-14 14:15:26
 */

import 'package:intl/intl.dart';

class TimeUtils {
  static String handleDate(String oldTime) {
    if (oldTime == '0') {
      return '未知';
    }
    String nowTime = DateTime.now().toString().split('.')[0].replaceAll('-', '/');

    int nowyear = int.parse(nowTime.split(" ")[0].split('/')[0]);
    int nowmonth = int.parse(nowTime.split(" ")[0].split('/')[1]);
    int nowday = int.parse(nowTime.split(" ")[0].split('/')[2]);
    int nowhour = int.parse(nowTime.split(" ")[1].split(':')[0]);
    int nowmin = int.parse(nowTime.split(" ")[1].split(':')[1]);

    int oldyear = int.parse(oldTime.split(" ")[0].split('/')[0]);
    int oldmonth = int.parse(oldTime.split(" ")[0].split('/')[1]);
    int oldday = int.parse(oldTime.split(" ")[0].split('/')[2]);
    int oldhour = int.parse(oldTime.split(" ")[1].split(':')[0]);
    int oldmin = int.parse(oldTime.split(" ")[1].split(':')[1]);

    var now = DateTime(nowyear, nowmonth, nowday, nowhour, nowmin);
    var old = DateTime(oldyear, oldmonth, oldday, oldhour, oldmin);
    var difference = now.difference(old);

    if (difference.inDays > 1) {
      return '昨天'.toString();
    } else if (difference.inDays == 1) {
      return '昨天'.toString();
    } else if (difference.inHours >= 1 && difference.inHours < 24) {
      return (difference.inHours).toString() + '小时前';
    } else if (difference.inMinutes > 5 && difference.inMinutes < 60) {
      return (difference.inMinutes).toString() + '分钟前';
    } else if (difference.inMinutes <= 5) {
      return '刚刚';
    } else {
      return '未知';
    }
  }

  static String readTimestampWithHandleDate(var timestamp) {
    try {
      if (timestamp == 0) {
        var format = DateFormat('yyyy/MM/dd HH:mm:ss');
        var time = '';
        var date = DateTime.now();
        time = format.format(date);
        return time;
      }
      var format = DateFormat('yyyy/MM/dd HH:mm:ss');
      var date = DateTime.fromMillisecondsSinceEpoch(timestamp.toInt() * 1000);
      var time = '';
      time = format.format(date);
      return time;
    } catch (e) {
      return '';
    }
  }

  static int currentTimeMillis() {
    return DateTime.now().millisecondsSinceEpoch;
  }

  /*
 * 当前时间戳 精确到秒
 */
  static String getNowTimeStamp() {
    var now = DateTime.now();
    var formatter = DateFormat('yyyy-MM-dd HH:mm:ss');
    String formatted = formatter.format(now);
    return formatted;
  }

  /*
 * 当前时间
 */
  static String getNowTime() {
    var now = DateTime.now();
    var formatter = DateFormat('yyyy-MM-dd');
    String formatted = formatter.format(now);
    return formatted;
  }

  /*
 * 字符串转时间戳
 */
  static int timeToMillSendonds(DateTime timestamp) {
    int time = ((timestamp.millisecondsSinceEpoch) / 1000).ceil();
    return time;
  }

  /*
 * 字符串转时间戳
 */
  static String timeToMillSendondsWithString(DateTime timestamp) {
    String time = ((timestamp.millisecondsSinceEpoch) / 1000).ceil().toString();
    return time;
  }

/*
 * 时间戳转换为时间 yyyy-MM-dd HH:mm
 */
  static String readTimestamp(var timestamp) {
    try {
      var format = DateFormat('yyyy-MM-dd');
      var date = DateTime.fromMillisecondsSinceEpoch(timestamp.toInt() * 1000);
      var time = '';
      time = format.format(date);
      return time;
    } catch (e) {
      return '';
    }
  }

  /*
 * 时间戳转换为时间 yyyy-MM-dd HH:mm
 */
  static String readDayTimetamp(var timestamp) {
    try {
      var format = DateFormat('yyyy-MM-dd HH:mm');
      var date = DateTime.fromMillisecondsSinceEpoch(timestamp.toInt() * 1000);
      var time = '';
      time = format.format(date);
      return time;
    } catch (e) {
      return '';
    }
  }

  /*
 * 时间戳转换为时间 yyyy-MM-dd HH:mm
 */
  static String readSecondTime(var timestamp) {
    try {
      if (timestamp == 0) {
        return "";
      }
      var format = DateFormat('yyyy-MM-dd HH:mm:ss');
      var date = DateTime.fromMillisecondsSinceEpoch(timestamp.toInt() * 1000);
      var time = '';
      time = format.format(date);
      return time;
    } catch (e) {
      return '';
    }
  }

  /*
 * 时间戳转换为时间 yyyy-MM-dd HH:mm
 */
  static String readMinTime(var timestamp) {
    try {
      if (timestamp == 0) {
        return "";
      }
      var format = DateFormat('yyyy-MM-dd HH:mm');
      var date = DateTime.fromMillisecondsSinceEpoch(timestamp.toInt() * 1000);
      var time = '';
      time = format.format(date);
      return time;
    } catch (e) {
      return '';
    }
  }

  static String currentTimeMillisString() {
    return DateTime.now().millisecondsSinceEpoch.toString();
  }

  /*
 * 时间戳转换为时间 yyyy年MM月
 */
  static String readMonthTime(var timestamp) {
    try {
      var format = DateFormat('yyyy年MM月');
      var date = DateTime.fromMillisecondsSinceEpoch(timestamp.toInt() * 1000);
      var time = '';
      time = format.format(date);
      return time;
    } catch (e) {
      return '';
    }
  }

  //时间差
  static String diffeerenceNowTime(var timestamp) {
    try {
      var _nowTime = DateTime.now();
      var _deleLine = DateTime.parse(readTimestamp(timestamp));
      var difference = _nowTime.difference(_deleLine);
      return formatDuration(Duration(seconds: difference.inSeconds));
    } catch (e) {
      return '';
    }
  }

  //当前时间切换
  static String formatDuration(Duration d) {
    var seconds = d.inSeconds;
    final days = seconds ~/ Duration.secondsPerDay;
    seconds -= days * Duration.secondsPerDay;
    final hours = seconds ~/ Duration.secondsPerHour;
    seconds -= hours * Duration.secondsPerHour;
    final minutes = seconds ~/ Duration.secondsPerMinute;
    seconds -= minutes * Duration.secondsPerMinute;

    final List<String> tokens = [];
    if (days != 0) {
      tokens.add('$days天');
    }
    if (tokens.isNotEmpty || hours != 0) {
      tokens.add('$hours小时');
    }
    // if (tokens.isNotEmpty || minutes != 0) {
    //   tokens.add('$minutes分');
    // }
    // tokens.add('$seconds秒');

    return tokens.join('') == "" ? "新项目" : tokens.join('');
  }

  //GMT转时间
  static String? readGMTTime(String timestamp) {
    List _timeList = [];
    try {
      timestamp = timestamp.replaceAll("Jan", "01");
      timestamp = timestamp.replaceAll("Feb", "02");
      timestamp = timestamp.replaceAll("Mar", "03");
      timestamp = timestamp.replaceAll("Apr", "04");
      timestamp = timestamp.replaceAll("May", "05");
      timestamp = timestamp.replaceAll("Jun", "06");
      timestamp = timestamp.replaceAll("Jul", "07");
      timestamp = timestamp.replaceAll("Aug", "08");
      timestamp = timestamp.replaceAll("Sep", "09");
      timestamp = timestamp.replaceAll("Oct", "10");
      timestamp = timestamp.replaceAll("Nov", "11");
      timestamp = timestamp.replaceAll("Dec", "12");
      _timeList = timestamp.split(" ");
      return _timeList[3] + '-' + _timeList[2] + '-' + _timeList[1];
    } catch (e) {
      return "";
    }
  }
}
